package thread;

import java.awt.*;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

class MyThreadPool1 {
    //工作队列
    BlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>(100);

    //由于这里不涉及到扩容啥的 所以我们就写一个固定的线程数 这个线程就是核心线程
    //在new这个线程池的同时将多线程创建出来
    public MyThreadPool1(int n) {
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(() -> {
                //创建出来的线程去工作队列中取线程然后去执行任务
                try {
                    while (true) {
                        Runnable task = workQueue.take();
                        task.run();
                    }

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.setDaemon(true);
            t.start();
        }
    }

    //提供submit方法来提交任务到工作队列当中
    public void submit(Runnable task) throws InterruptedException {
        workQueue.put(task);
    }


}


public class demo9 {

    public static void main(String[] args) throws InterruptedException {
        MyThreadPool1 myThreadPool1 = new MyThreadPool1(5);
        for (int i = 0; i < 100; i++) {
            int id = i;
            myThreadPool1.submit(() -> {
                Thread curThread = Thread.currentThread();
                System.out.println("哈喽" + curThread.getName() + "," + "id=" + id);
            });
        }
        Thread.sleep(1000);
    }


}
